Imports System.Data.SqlClient
Imports System.Data
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class ArticuloDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function registrarArticulo(ByVal art As Articulo) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "USP_RegistrarArticulo"
            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@ARTCOD", art.ART_Codigo)
                .Parameters.AddWithValue("@ARTDES", art.ART_Descripcion)
                .Parameters.AddWithValue("@FAMCOD", art.FAM_Codigo)
                .Parameters.AddWithValue("@UNMCOD", art.UNM_Codigo)
                .Parameters.AddWithValue("@ARTCLAR", art.ART_CantLargo)
                .Parameters.AddWithValue("@ACT", art.ART_Activo)
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function actualizarArticulo(ByVal art As Articulo) As Boolean
        Dim valor As Boolean = False
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "USP_ActualizarArticulo"
            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@ARTCOD", art.ART_Codigo)
                .Parameters.AddWithValue("@ARTDES", art.ART_Descripcion)
                .Parameters.AddWithValue("@FAMCOD", art.FAM_Codigo)
                .Parameters.AddWithValue("@UNMCOD", art.UNM_Codigo)
                .Parameters.AddWithValue("@ARTCLAR", art.ART_CantLargo)
                .Parameters.AddWithValue("@ACT", art.ART_Activo)
            End With

            valor = True
        Catch ex As Exception
            valor = False
        Finally
            con.Close()
        End Try

        Return valor
    End Function

    Function obtenerListadoArticulo() As DataSet
        Dim ds As New DataSet
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()
            Dim query As String = "SELECT * FROM VW_Articulo"
            Dim adapter As New SqlDataAdapter(query, con)
            adapter.Fill(ds)
        Catch ex As Exception
            ds = Nothing
        Finally
            con.Close()
        End Try

        Return ds
    End Function

    Function obtenerArticulo(ByVal codArticulo As String) As Articulo
        Dim articulo As Articulo = Nothing
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()

            Dim query As String = "SELECT * FROM VW_Articulo WHERE ART_Codigo = @ARTCOD"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@ARTCOD", codArticulo)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                articulo = New Articulo(ds.Tables(0).Rows(0)("ART_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("ART_Descripcion").ToString.Trim, ds.Tables(0).Rows(0)("FAM_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("UNM_Codigo").ToString.Trim, _
                                        CInt(ds.Tables(0).Rows(0)("ART_CantLargo").ToString.Trim), ds.Tables(0).Rows(0)("ART_Activo").ToString.Trim)
            Else
                articulo = New Articulo("-1", ds.Tables(0).Rows(0)("ART_Descripcion").ToString.Trim, ds.Tables(0).Rows(0)("FAM_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("UNM_Codigo").ToString.Trim, _
                                        CInt(ds.Tables(0).Rows(0)("ART_CantLargo").ToString.Trim), ds.Tables(0).Rows(0)("ART_Activo").ToString.Trim)
            End If
        Catch e As Exception
            articulo = Nothing
        Finally
            con.Close()
        End Try

        Return articulo
    End Function

End Class
